package com.zxt.dao.impl;

import com.zxt.dao.GoodsTypeDao;
import com.zxt.domain.GoodsType;
import com.zxt.util.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public List<GoodsType> select(int level) {
        String sql="SELECT id,NAME,LEVEL,parent FROM tb_goods_type WHERE LEVEL=?";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(GoodsType.class),level);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public GoodsType selectById(Integer typeId) {
        String sql="SELECT id,NAME,LEVEL,parent FROM tb_goods_type WHERE id=?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(GoodsType.class),typeId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
